﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ext.Net;
using System.ComponentModel;
using System.Web.UI;
using System.Xml.Serialization;
using Newtonsoft.Json;
using System.Web;

namespace Sio.Mdm.Web.Controls
{
    [Meta]
    public class Window : Ext.Net.WindowBase
    {
        public Window()
        {
        }

        public override string InstanceOf
        {
            get
            {
                return "Mdm.Window";
            }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [XmlIgnore]
        [JsonIgnore]
        public override ConfigOptionsCollection ConfigOptions
        {
            get
            {
                ConfigOptionsCollection list = base.ConfigOptions;

                list.Add("listeners", new ConfigOption("listeners", new SerializationOptions("listeners", JsonMode.Object), null, this.Listeners));
                list.Add("directEvents", new ConfigOption("directEvents", new SerializationOptions("directEvents", JsonMode.Object), null, this.DirectEvents));

                return list;
            }
        }

        private WindowProxyListeners listeners;
        [Meta]
        [ConfigOption("listeners", JsonMode.Object)]
        [Category("2. Observable")]
        [NotifyParentProperty(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [ViewStateMember]
        [Description("Client-side JavaScript Event Handlers")]
        public WindowProxyListeners Listeners
        {
            get
            {
                if (this.listeners == null)
                {
                    this.listeners = new WindowProxyListeners();
                }

                return this.listeners;
            }
        }

        private WindowProxyDirectEvents directEvents;
        /// <summary>
        /// Server-side Ajax Event Handlers
        /// </summary>
        [Meta]
        [Category("2. Observable")]
        [NotifyParentProperty(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [ConfigOption("directEvents", JsonMode.Object)]
        [ViewStateMember]
        [Description("Server-side Ajax Event Handlers")]
        public WindowProxyDirectEvents DirectEvents
        {
            get
            {
                if (this.directEvents == null)
                {
                    this.directEvents = new WindowProxyDirectEvents();
                }

                return this.directEvents;
            }
        }
    }

    [Meta]
    public class WindowProxy : Ext.Net.Component
    {
        public WindowProxy()
        {
            Width = Height = 0;
            Hidden = true;
        }

        [Meta]
        [ConfigOption("windowId")]
        [Category("6. Panel")]
        [DefaultValue(null)]
        [NotifyParentProperty(true)]
        [Description("Id of parent window to monitor events on.")]
        public string WindowId { get; set; }

        /// <summary>
        /// Overriden to create cutom instance on client side
        /// </summary>
        public override string InstanceOf
        {
            get
            {
                return "Mdm.WindowProxy";
            }
        }

        [Meta]
        [DirectEventUpdate(MethodName = "SetTitle")]
        [ConfigOption]
        [Category("6. Panel")]
        [DefaultValue("")]
        [Localizable(true)]
        [NotifyParentProperty(true)]
        [Description("The title text to display in the panel header (defaults to ''). When a title is specified the header element will automatically be created and displayed unless header is explicitly set to false. If you don't want to specify a title at config time, but you may want one later, you must either specify a non-empty title (a blank space ' ' will do) or header:true so that the content Container element will get created.")]
        public virtual string Title
        {
            get
            {
                return (string)this.ViewState["Title"] ?? "";
            }
            set
            {
                this.ViewState["Title"] = value;
            }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [XmlIgnore]
        [JsonIgnore]
        public override ConfigOptionsCollection ConfigOptions
        {
            get
            {
                ConfigOptionsCollection list = base.ConfigOptions;

                list.Add("windowId", new ConfigOption("windowId", new SerializationOptions("windowId"), null, this.WindowId));
                list.Add("listeners", new ConfigOption("listeners", new SerializationOptions("listeners", JsonMode.Object), null, this.Listeners));
                list.Add("directEvents", new ConfigOption("directEvents", new SerializationOptions("directEvents", JsonMode.Object), null, this.DirectEvents));

                return list;
            }
        }

        private WindowProxyListeners listeners;
        [Meta]
        [ConfigOption("listeners", JsonMode.Object)]
        [Category("2. Observable")]
        [NotifyParentProperty(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [ViewStateMember]
        [Description("Client-side JavaScript Event Handlers")]
        public WindowProxyListeners Listeners
        {
            get
            {
                if (this.listeners == null)
                {
                    this.listeners = new WindowProxyListeners();
                }

                return this.listeners;
            }
        }

        private WindowProxyDirectEvents directEvents;
        /// <summary>
        /// Server-side Ajax Event Handlers
        /// </summary>
        [Meta]
        [Category("2. Observable")]
        [NotifyParentProperty(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [ConfigOption("directEvents", JsonMode.Object)]
        [ViewStateMember]
        [Description("Server-side Ajax Event Handlers")]
        public WindowProxyDirectEvents DirectEvents
        {
            get
            {
                if (this.directEvents == null)
                {
                    this.directEvents = new WindowProxyDirectEvents();
                }

                return this.directEvents;
            }
        }

        /// <summary>
        /// Sets the title text for the panel and optionally the icon class.
        /// </summary>
        [Meta]
        [Description("Sets the title text for the panel and optionally the icon class.")]
        public virtual void SetTitle(string title)
        {
            this.Call("setTitle", title);
        }

        /// <summary>
        /// Sets the title text for the panel and optionally the icon class.
        /// </summary>
        [Meta]
        [Description("Sets the title text for the panel and optionally the icon class.")]
        public virtual void SetTitle(string title, string cls)
        {
            this.Call("setTitle", title, cls);
        }

        [Meta]
        public virtual void Close()
        {
            this.Call("close");
        }

        [Meta]
        public void AddParameter(Ext.Net.Parameter parameter)
        {
            this.Call("addParameter", parameter.Name, parameter.Value);
        }

        [Meta]
        public void NotifyParent(params object[] args)
        {
            this.Call("callParent", args);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!ExtNet.IsAjaxRequest)
            {
                DirectEvents.ButtonClicked.ExtraParams.Add(
                    new Parameter("buttonType", "item.buttonType", ParameterMode.Raw)
                );
            }

            base.OnLoad(e);
        }
    }

    public class WindowProxyDirectEvents : WindowDirectEvents
    {
        private ComponentDirectEvent buttonClicked;

        /// <summary>
        /// Fires after the window has been maximized.
        /// </summary>
        [ListenerArgument(0, "item", typeof(Button), "item")]
        [TypeConverter(typeof(ExpandableObjectConverter))]
        [ConfigOption("buttonClicked", typeof(DirectEventJsonConverter))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [NotifyParentProperty(true)]
        [Description("Fires after the window button has been clicked.")]
        public virtual ComponentDirectEvent ButtonClicked
        {
            get
            {
                if (this.buttonClicked == null)
                {
                    this.buttonClicked = new ComponentDirectEvent();
                }

                return this.buttonClicked;
            }
        }

        private ComponentDirectEvent clientNotify;

        /// <summary>
        /// Fires after the window has been maximized.
        /// </summary>        
        [ListenerArgument(0, "item", typeof(Window), "this")]
        [ListenerArgument(1, "a", typeof(string), "item")]
        [ListenerArgument(2, "b", typeof(string), "item")]
        [ListenerArgument(3, "c", typeof(string), "item")]
        [ListenerArgument(4, "d", typeof(string), "item")]
        [ListenerArgument(5, "e", typeof(string), "item")]
        [ListenerArgument(6, "f", typeof(string), "item")]
        [ListenerArgument(7, "g", typeof(string), "item")]
        [TypeConverter(typeof(ExpandableObjectConverter))]
        [ConfigOption("clientNotify", typeof(DirectEventJsonConverter))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [NotifyParentProperty(true)]
        [Description("Fires after the window button has been clicked.")]
        public virtual ComponentDirectEvent ClientNotify
        {
            get
            {
                if (this.clientNotify == null)
                {
                    this.clientNotify = new ComponentDirectEvent();
                }

                return this.clientNotify;
            }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [XmlIgnore]
        [JsonIgnore]
        public override ConfigOptionsCollection ConfigOptions
        {
            get
            {
                ConfigOptionsCollection list = base.ConfigOptions;

                list.Add("buttonClicked", new ConfigOption("buttonClicked", new SerializationOptions("buttonClicked", typeof(DirectEventJsonConverter)), null, this.ButtonClicked));
                list.Add("clientNotify", new ConfigOption("clientNotify", new SerializationOptions("clientNotify", typeof(DirectEventJsonConverter)), null, this.ClientNotify));

                return list;
            }
        }
    }

    public class WindowProxyListeners : WindowListeners
    {
        private ComponentListener buttonClicked;

        /// <summary>
        /// Fires after the window has been maximized.
        /// </summary>
        [ListenerArgument(0, "item", typeof(Button), "item")]
        [TypeConverter(typeof(ExpandableObjectConverter))]
        [ConfigOption("buttonClicked", typeof(ListenerJsonConverter))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [NotifyParentProperty(true)]
        [Description("Fires after the window button has been clicked.")]
        public virtual ComponentListener ButtonClicked
        {
            get
            {
                if (this.buttonClicked == null)
                {
                    this.buttonClicked = new ComponentListener();
                }

                return this.buttonClicked;
            }
        }

        private ComponentListener clientNotify;

        /// <summary>
        /// Fires after the window has been maximized.
        /// </summary>        
        [ListenerArgument(0, "item", typeof(Window), "this")]
        [ListenerArgument(1, "a", typeof(string), "item")]
        [ListenerArgument(2, "b", typeof(string), "item")]
        [ListenerArgument(3, "c", typeof(string), "item")]
        [ListenerArgument(4, "d", typeof(string), "item")]
        [ListenerArgument(5, "e", typeof(string), "item")]
        [ListenerArgument(6, "f", typeof(string), "item")]
        [ListenerArgument(7, "g", typeof(string), "item")]
        [TypeConverter(typeof(ExpandableObjectConverter))]
        [ConfigOption("clientNotify", typeof(ListenerJsonConverter))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [NotifyParentProperty(true)]
        [Description("Fires when page loaded into body has something to send to parent window.")]
        public virtual ComponentListener ClientNotify
        {
            get
            {
                if (this.clientNotify == null)
                {
                    this.clientNotify = new ComponentListener();
                }

                return this.clientNotify;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [XmlIgnore]
        [JsonIgnore]
        public override ConfigOptionsCollection ConfigOptions
        {
            get
            {
                ConfigOptionsCollection list = base.ConfigOptions;

                list.Add("buttonClicked", new ConfigOption("buttonClicked", new SerializationOptions("buttonClicked", typeof(ListenerJsonConverter)), null, this.ButtonClicked));
                list.Add("clientNotify", new ConfigOption("clientNotify", new SerializationOptions("clientNotify", typeof(ListenerJsonConverter)), null, this.ClientNotify));

                return list;
            }
        }
    }

}
